package com.hyzx.qbasic.common.util;

import java.util.regex.Pattern;

/**
 * Created by 黄江华 on 2018/7/18
 */

public class RegUtils {

    /**
     * 正则表达式：验证手机号
     */
    public static final Pattern REGEX_MOBILE = Pattern.compile("^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

    /**
     * 正则表达式：验证邮箱
     */
    public static final Pattern REGEX_EMAIL = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");

    /**
     * 正则表达式：验证汉字
     */
    public static final Pattern REGEX_CHINESE = Pattern.compile("^[\u4e00-\u9fa5],{0,}$");

    /**
     * 正则表达式：验证身份证
     */
    public static final Pattern REGEX_ID_CARD = Pattern.compile("(^\\d{18}$)|(^\\d{15}$)");

    /**
     * 正则表达式：验证URL
     */
    public static final Pattern REGEX_URL = Pattern.compile("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");

    /**
     * 正则表达式：验证IP地址
     */
    public static final Pattern REGEX_IP_ADDRESS = Pattern.compile("(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)");

    /**
     * 校验手机号
     */
    public static boolean isMobile(String mobile) {
        return REGEX_MOBILE.matcher(mobile).matches();
    }

    /**
     * 校验邮箱
     */
    public static boolean isEmail(String email) {
        return REGEX_EMAIL.matcher(email).matches();
    }

    /**
     * 校验汉字
     */
    public static boolean isChinese(String chinese) {
        return REGEX_CHINESE.matcher(chinese).matches();
    }

    /**
     * 校验身份证
     */
    public static boolean isIDCard(String idCard) {
        return REGEX_ID_CARD.matcher(idCard).matches();
    }

    /**
     * 校验URL
     */
    public static boolean isUrl(String url) {
        return REGEX_URL.matcher(url).matches();
    }

    /**
     * 校验IP地址
     */
    public static boolean isIPAddress(String ip) {
        return REGEX_IP_ADDRESS.matcher(ip).matches();
    }
}
